package 最佳买卖股票时机含冷冻期;
//在Solution1的基础上空间复杂度优化
public class Soultion2 {
    public int maxProfit(int[] prices) {
        if(prices.length==0){
            return 0;
        }
        int n = prices.length;
        int[][] state = new int[n][3];

        int f0 = -prices[0];
        int f1 = 0;
        int f2 = 0;
        for(int i = 1 ; i < n ; i++){
            int newf0 = Math.max(f0,f2-prices[i]);
            int newf1 = f0+prices[i];
            int newf2 = Math.max(f2,f1);
            f0 = newf0;
            f1 = newf1;
            f2 = newf2;
        }
        return Math.max(f1,f2);
    }
}
